#include "StdAfx.h"
#include "Galaxy.h"


Galaxy::Galaxy(void)
{
}

Galaxy::Galaxy(LPDIRECT3DDEVICE9 pDevice)
{
	_pDevice = pDevice;
}

void Galaxy::Create()
{
	LPD3DXMESH pTempEarthMesh = NULL;
	LPD3DXMESH pTempSunMesh   = NULL;
	LPD3DXMESH pTempMoonMesh = NULL;
	LPD3DXMESH pTempShipMesh = NULL;

	Star *sun = new Star("sun",NULL,_pDevice);
	Star* earth = new Star("earth",sun,_pDevice);
	Star* moon = new Star("moon",earth,_pDevice);

	sun->Draw(1.0f, 100, 20, pTempSunMesh,D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ));
	earth->Draw(0.5f, 100, 20, pTempEarthMesh,D3DCOLOR_COLORVALUE( 1.0, 1.0, 0.0, 1.0 ));
	moon->Draw(0.25f,200, 20, pTempMoonMesh, D3DCOLOR_COLORVALUE( 1.0, 0.0, 1.0, 1.0 ));

	srand((unsigned)time(0));
	int base = rand();

	outputDebug("sun:%f", sun->GetMaxChildrenDistance());
	outputDebug("earth:%f", earth->GetMaxChildrenDistance());
	outputDebug("moon:%f", moon->GetMaxChildrenDistance());
	
	sun->SetPos(D3DXVECTOR3(base%4/2, base%4/2, base%4/2));
	for (int i=0; i < sun->GetChildren().size(); i++)
	{
		for (; base%4 == 0; base = rand())
		{
		}
		Star* pson = sun->GetChildren()[i];
		pson->SetPos(D3DXVECTOR3(sun->GetRadius() + sun->GetMaxChildrenDistance(),
			0.0f,
			sun->GetRadius() + sun->GetMaxChildrenDistance()));
		for (int j=0; j < pson->GetChildren().size(); j++)
		{
			Star* pgrandson = pson->GetChildren()[j];
			for (; base%4 == 0; base = rand())
			{
			}
			pgrandson->SetPos(D3DXVECTOR3(pson->GetRadius() + pson->GetMaxChildrenDistance(),
				0.0f,
				pson->GetRadius() + pson->GetMaxChildrenDistance()));
		}
		//_pos.x = _pParent->GetPos().x + _pParent->GetRadius() + base%4/3.0f;
		//_pos.y = _pParent->GetPos().y;// + base%4/3;
		//_pos.z = _pParent->GetPos().z + _pParent->GetRadius() + base%4/3.0f;
	}
	//if (_pParent != NULL)
	//{
	//	if (_pParent->GetParent() != NULL)
	//	{
	//		for (; base%4 == 0; base = rand())
	//		{
	//		}
	//		_pos.x = _pParent->GetPos().x + _pParent->GetRadius() - 0.5f;//base%4/4;
	//		_pos.y = 0.0f;//_pParent->GetPos().y;// + base%4/4;
	//		_pos.z = _pParent->GetPos().z + _pParent->GetRadius() - 0.5f;//base%4/4;
	//	}
	//	else
	//	{
	//		for (; base%4 == 0; base = rand())
	//		{
	//		}
	//		_pos.x = _pParent->GetPos().x + _pParent->GetRadius() + base%4/3.0f;
	//		_pos.y = _pParent->GetPos().y;// + base%4/3;
	//		_pos.z = _pParent->GetPos().z + _pParent->GetRadius() + base%4/3.0f;
	//	}
	//}
	//else
	//{
	//	for (; base == 0; base = rand())
	//	{
	//	}
	//	_pos.x = base%4/2;
	//	_pos.y = base%4/2;
	//	_pos.z = base%4/2;
	//}
	//DebugVertor3(_pos);

	_stars.push_back(sun);
	_stars.push_back(earth);
	_stars.push_back(moon);
}

void Galaxy::Render(float passedTime)
{
	for (int i=0;i<_stars.size();i++)
	{
		_stars[i]->Render(passedTime);
	}
}

Galaxy::~Galaxy(void)
{
}
